﻿package UIFree
{
	public class uif_transition extends Object
	{
		public function uif_transition() {
			super();
		}
		
		public static function Pow(p: Number, x: Array=null): Number {
			return Math.pow(p, (x && x[0])?x[0]:6);
		}
	
		public static function Expo(p: Number, p2: Number=0): Number {
			return Math.pow(2, 8 * (p - 1));
		}
	
		public static function Circ(p: Number, p2: Number=0): Number {
			return 1 - Math.sin(Math.acos(p));
		}
	
		public static function Sine(p: Number, p2: Number=0): Number {
			return 1 - Math.sin((1 - p) * Math.PI / 2);
		}
	
		public static function Back(p: Number, lx: Number=2): Number {
			return Math.pow(p, 2) * ((lx + 1) * p - lx);
		}
		
		public static function line(p: Number, p2: Number=0): Number {
			return p;
		}
	
		public static function Bounce(p: Number, p2: Number=0): Number {
			var value: Number;
			for (var a: Number = 0, b: Number = 1; 1; a += b, b /= 2){
				if (p >= (7 - 4 * a) / 11){
					value = b * b - Math.pow((11 - 6 * a - 11 * p) / 4, 2);
					break;
				}
			}
			return value;
		}
		
		public static function Envelope(p: Number, p2: Number=0): Number {
			var attack: Number = 0.2;
			var fading: Number = 0.4;
			var shelf: Number = 0.8;
			var result: Number = shelf;
			if (p < attack) result = p/attack;
			else if (p < fading) result = shelf + (1 - p/fading) * (1 - shelf);
			return result;
		}
	
		public static function Elastic(p: Number, x: Number=1): Number {
			return Math.pow(2, 10 * --p) * Math.cos(20 * p * Math.PI * x/ 3);
		}
		
		public static function Default(p: Number, p2: Number=0): Number {
			return -(Math.cos(Math.PI * p) - 1) / 2;
		}
		
		protected static function plimit(p: Number): Number {
			if (p < 0) p = 0;
			else if (p > 1) p = 1;
			return p;
		} 
		
		public static function easeInOut(transition: Function=null, param:*=null): Function {
			transition = (transition != null)?transition:Default;
			return function(p: Number): Number {
				p = plimit(p);
				if (p < 0.5) return transition(p * 2, param) / 2;
				else return 1 - transition(2 * (1 - p), param) / 2;
			}
		}
		
		public static function easeIn(transition: Function=null, param:*=null): Function {
			transition = (transition != null)?transition:Default;
			return function(p: Number): Number {return transition(plimit(p), param)};
		}
		
		public static function easeOut(transition: Function=null, param:*=null): Function {
			transition = (transition != null)?transition:Default;
			return function(p: Number): Number {
				return 1 - transition(1 - plimit(p), param);
			};
		}
		
		public static function vibration(transition: Function=null, param:*=null): Function {
			transition = (transition != null)?transition:Default;
			return function(p: Number): Number {
				p = plimit(p);				
				if (p < 0.5) return transition(p * 2, param);
				else return transition((1 - p) * 2, param);
			};
		}
	}
}